# -*- coding: utf-8 -*-
"""
Created on Fri Sep 27 14:54:58 2024

@author: Unknown
"""

import numpy as np
import pandas as pd
#数据堆叠concat
df1=pd.DataFrame([[10,20],[20,30]],columns=['A','B'])
df2=pd.DataFrame([[40,50],[50,60]],columns=['B','C'])
re1=pd.concat([df1,df2],axis=0)
re2=pd.concat([df1,df2],axis=1)
print(re1)
print(re2)
#数据合并
df3=pd.DataFrame([[10,20,30],[20,30,40],[30,40,50]],columns=['A','B','C'])
df4=pd.DataFrame([[10,20,60],[30,40,70],[20,40,90]],columns=['A','B','D'])
#左连接
print(pd.merge(df3, df4,how="left",on=["A","B"]))
#右连接
print(pd.merge(df3, df4,how="right",on=["A","B"]))
print(pd.merge(df4, df3,how="left",on=["A","B"]))
#内连接  取交集
print(pd.merge(df3, df4,how="inner",on=["A","B"]))
#外连接  取并集
print(pd.merge(df3, df4,how="outer",on=["A","B"]))

#数据清洗
sources={'姓名':['张三','李四','王五','张三'],
         '语文':[84,92,87,84],
         '数学':[89,90,95,89],
         '英语':[90,81,99,69],
         '计算机':[95,89,97,78]}
df=pd.DataFrame(sources)
#duplicated 返回的是是否重复的布尔值
print(df.duplicated(subset=['姓名']))
#drop_duplicates 返回的是去重后的对象，keep表示保留重复值出现的第几次记录
print(df.drop_duplicates(subset=['姓名'],keep='first'))
